﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BO;
using Entity;

namespace Final_Prototype_UsingDev_QuanLy_071012
{
    public partial class Form_Custom_Management_Edit : Form
    {
        private Main _frCustomerManagement;
        private Form_Custom_Authorized _frCustomerAuthorized;

        public string _received;

        public Form_Custom_Management_Edit(Main frCustomerManagement)
        {
            InitializeComponent();
            _frCustomerManagement = frCustomerManagement;
        }

        public Form_Custom_Management_Edit(Form_Custom_Authorized frCustomerAuthorized)
        {
            InitializeComponent();
            _frCustomerAuthorized = frCustomerAuthorized;
        }
 

        /// <summary>
        /// Dien thong tin khach hang vao cac field
        /// </summary>
        public void FillInfo(string received)
        {
            _received = received;          
        }

        private void LoadCustomer(string received)
        {
            CustomerBO csBO = new CustomerBO();
            CustomerEntity csInfo = csBO.getCustomerInfo(received);

            cbxSalename.Text = csInfo.StaffName;
            txtCompany.Text = csInfo.CompanyName;
            txtContact.Text = csInfo.ContactPerson;
            cbxDomain.Text = csInfo.Domain;
            txtCode.Text = csInfo.CustomerCode;
            txtTax.Text = csInfo.TaxCode;
            cboCry.Text = csInfo.Country;
            cboCti.Text = csInfo.Province;
            cboPce.Text = csInfo.District;
            rtbAdd.Text = csInfo.Address;
            txtMai.Text = csInfo.Mail;
            txtWeb.Text = csInfo.Web;
            txtTel.Text = csInfo.Tel;
            txtFax.Text = csInfo.Fax;
            rtbRmk.Text = csInfo.Remark;
        }
        /// <summary>
        /// Save thong tin khach hang
        /// </summary>
        private void SaveCustomerEdited()
        {
            CustomerBO csBO = new CustomerBO();
            CustomerEntity csInfo = new CustomerEntity();

            csInfo.StaffName = cbxSalename.Text;
            csInfo.CompanyName = txtCompany.Text;
            csInfo.ContactPerson = txtContact.Text;
            csInfo.Domain = cbxDomain.Text;
            csInfo.CustomerCode = txtCode.Text;
            csInfo.TaxCode = txtTax.Text;
            csInfo.Country = cboCry.Text;
            csInfo.Province = cboCti.Text;
            csInfo.District = cboPce.Text;
            csInfo.Address = rtbAdd.Text;
            csInfo.Mail = txtMai.Text;
            csInfo.Web = txtWeb.Text;
            csInfo.Tel = txtTel.Text;
            csInfo.Fax = txtFax.Text;
            csInfo.Remark = rtbRmk.Text;

            csBO.EditCustomer(csInfo);
            MessageBox.Show("Save successful!");
        }

        /// <summary>
        /// Load country len combobox
        /// </summary>
        private void LoadCountry()
        {
            CountryBO cBO = new CountryBO();
            cboCry.DataSource = cBO.LoadCountry();
            cboCry.DisplayMember = "CountryName";
            cboCry.SelectedIndex = 202;
        }

        /// <summary>
        /// Load province len combobox
        /// </summary>
        private void LoadProvince()
        {
            ProvinceBO pBO = new ProvinceBO();
            string CountryName = cboCry.Text;
            cboCti.DataSource = pBO.LoadProvince(CountryName);
            cboCti.DisplayMember = "Province";
           
        }

        /// <summary>
        /// Load danh sach cac contact cua khach hang
        /// </summary>
        private void LoadContact()
        {
            ContactBO cBO = new ContactBO();
            grdContact.DataSource= cBO.LoadContact(txtCode.Text.Trim());
        }

        /// <summary>
        /// Load danh sach quotation
        /// </summary>
        private void LoadQuotation()
        {
            QuotationBO qBO = new QuotationBO();
            grdQuotation.DataSource = qBO.LoadQuotation(txtCompany.Text.Trim());
        }

        /// <summary>
        /// Load danh sach sale cu
        /// </summary>
        private void LoadHistorySale()
        {
            ManagedByBO mBO = new ManagedByBO();
            grdBackUpList.DataSource = mBO.LoadHistorySale(txtCompany.Text.Trim());
        }
        /// <summary>
        /// Load district combobox
        /// </summary>
        private void LoadDistrict()
        {
            DistrictBO dBO = new DistrictBO();
            string ProvinceName = cboCti.Text;
            cboPce.DataSource = dBO.LoadDistrict(ProvinceName);
            cboPce.DisplayMember = "DistrictName";
        }

        /// <summary>
        /// Load ten sale len combobox
        /// </summary>
        private void LoadDomain()
        {
            DomainBO dBO = new DomainBO();
            cbxDomain.DataSource = dBO.LoadDomain();
            cbxDomain.DisplayMember = "DomainName";

        }

        private bool CheckCode(string code)
        {
            CustomerBO csBO = new CustomerBO();
            return csBO.CheckCode(code);
        }

        private void txtCode_TextChanged(object sender, EventArgs e)
        {
           
        }


        private void txtTax_TextChanged(object sender, EventArgs e)
        {
           
        }

        private void txtCus_TextChanged(object sender, EventArgs e)
        {
           

        }

        private void txtCom_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void cboCry_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        private void cboCti_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDistrict();
        }

        private void cboPce_SelectedIndexChanged(object sender, EventArgs e)
        {
           
        }

        private void rtbAdd_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void txtTel_TextChanged(object sender, EventArgs e)
        {
            
        }
       
        private void Form_Custom_Management_Edit_Load(object sender, EventArgs e)
        {
            LoadCountry();
            LoadProvince();
            LoadCustomer(_received);            
            cbxSalename.Enabled = false;
            LoadContact();
            LoadQuotation();
            LoadHistorySale();
            LoadDistrict();
            LoadDomain();
 
            
        }

        private void grpCus_Enter(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void btnSav_Click(object sender, EventArgs e)
        {
            SaveCustomerEdited();
            _frCustomerManagement.LoadCustomer();
        }
    }
}
